﻿using NDK.Module.BLL;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.MachineManager;
using NDK.Motion.Monitor;
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;

namespace NDK.Module.StationLogic
{
    [Monitor(Description = "翻转取料")]
    public class FlipStationPut : StationLogic
    {
        #region 通用
        //记录当前步骤，用于步骤变化的比较
        private StationStep _curStep = StationStep.开始准备;
        //对外提供工位的步骤变化事件
        public override event PropertyChangedEventHandler PropertyChanged;
        //对外提供唯一实例
        public static FlipStationPut Instance = new FlipStationPut();
        //私有化的构造函数
        private FlipStationPut () : base()
        {
            this.Name = "翻转取料";
        }
        //步骤字段、属性
        private StationStep _step = StationStep.开始准备;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _step;
            }
            set
            {
                if(_curStep != value)
                {
                    _step = value;
                    _curStep = value;

                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init ()
        {
            base.Init();
            Step = StationStep.开始准备;
            logBaseInfo = "FlipStationPut";
        }

        private bool VacuumOn ()
        {
            return VacuumMethod(true, (Machine.do_翻转平台真空1吸, Machine.di_翻转平台真空1反馈, true));
        }

        public void VacuumOff ()
        {
            Machine.do_翻转平台真空1吸.OFF();
            Machine.do_翻转平台真空1破.ON();
            Thread.Sleep(150);
            Machine.do_翻转平台真空1破.ON();
        }

        /// <summary>
        /// 工站动作流程
        /// </summary>
        /// <param name="mode"></param>
        public override void ActionProcess ()
        {
            try
            {
                switch(Step)
                {
                    case StationStep.开始准备:
                        Step = StationStep.流程开始;
                        break;
                    case StationStep.流程开始:
                        if(Common.isWriteLog)
                        {
                            logCtStr = string.Empty;
                            swStepTime.Restart();
                            swStationCT.Restart();
                            logCtStr += $"{logBaseInfo},TransMove工站开始,{swStepTime.ElapsedMilliseconds},{Environment.NewLine}";
                        }
                        Step = StationStep.轴初始位;
                        break;

                    case StationStep.轴初始位:
                        if(MotionControl.AxesMove(Machine.pos_Rotate_初始位))
                        {
                            工位要料 = true;
                            Step = StationStep.等待上工位完成;
                        }
                        break;

                    case StationStep.等待上工位完成:
                        if(LoadingTrans2.Instance.工位完成)
                        {
                            LoadingTrans2.Instance.VacuumOff();
                            Step = StationStep.打开真空;
                        }
                        break;

                    case StationStep.打开真空:
                        if(VacuumOn())
                        {
                            工位要料 = false;
                            Step = StationStep.克隆数据;
                        }
                        break;

                    case StationStep.克隆数据:
                        Step = StationStep.等待上工位关完成;
                        break;

                    case StationStep.等待上工位关完成:
                        if(!LoadingTrans2.Instance.工位完成)
                        {
                            Step = StationStep.等待下工位开要料;
                        }
                        break;

                    case StationStep.等待下工位开要料:
                        if(FlipStationCatch.Instance.工位要料)
                        {
                            Step = StationStep.轴翻转位;
                        }
                        break;

                    case StationStep.轴翻转位:
                        if(MotionControl.AxesMove(Machine.pos_Rotate_翻转位))
                        {
                            工位完成 = true;
                            Step = StationStep.等待下工位关要料;
                        }
                        break;

                    case StationStep.等待下工位关要料:
                        if(!FlipStationCatch.Instance.工位要料)
                        {
                            Step = StationStep.轴到原位;
                        }
                        break;

                    case StationStep.轴到原位:
                        if(MotionControl.AxesMove(Machine.pos_Rotate_初始位))
                        {
                            工位完成 = false;
                            Step = StationStep.判断模式;
                        }
                        break;

                    case StationStep.判断模式:
                        if((!Common.GRR循环模式 || Common.GRREnd) && !Common.pointCheck)
                        {
                            Step = StationStep.流程结束;
                        }
                        else
                        {
                            GRR工位要料 = true;
                            Step = StationStep.GRR等待下工位完成;
                        }
                        break;

                    case StationStep.GRR等待下工位完成:
                        if(FlipStationCatch.Instance.GRR工位完成)
                        {
                            Step = StationStep.GRR翻转到位;
                        }
                        break;

                    case StationStep.GRR翻转到位:
                        if(MotionControl.AxesMove(Machine.pos_Rotate_翻转位))
                        {
                            FlipStationCatch.Instance.VacuumOff();
                            Step = StationStep.GRR打开真空吸;
                        }
                        break;

                    case StationStep.GRR打开真空吸:
                        if(VacuumOn())
                        {
                            Step = StationStep.GRR翻转到原位;
                        }
                        break;

                    case StationStep.GRR翻转到原位:
                        if(MotionControl.AxesMove(Machine.pos_Rotate_初始位))
                        {
                            GRR工位要料 = false;
                            Step = StationStep.GRR再次判断真空;
                        }
                        break;

                    case StationStep.GRR再次判断真空:
                        if(VacuumOn())
                        {
                            GRR工位完成 = true;
                            Step = StationStep.GRR下工位关闭要料;
                        }
                        break;

                    case StationStep.GRR下工位关闭要料:
                        if(!LoadingTrans2.Instance.GRR工位要料)
                        {
                            GRR工位完成 = false;
                            Step = StationStep.流程结束;
                        }
                        break;

                    case StationStep.流程结束:
                        Common.sw7.Stop();
                        if(Common.isWriteLog)
                        {
                            logCtStr += $"{logBaseInfo},TransMove结束,{swStepTime.ElapsedMilliseconds},{Environment.NewLine}";
                            logCtStr += $"{logBaseInfo},TransMove结束,{swStationCT.ElapsedMilliseconds},{Environment.NewLine}";
                            PublicMethod.WriteLogToTxt(logCtStr, $"000StepCT_{DateTime.Now.ToString("yyyyMMdd")}", "5.TransMove");
                        }
                        Step = StationStep.开始准备;
                        break;
                    default:
                        break;

                }
            }
            catch(Exception ex)
            {
                UILog_Error($"[FlipStationPut ActionProcess Thread ERROR] {ex.Message}");
            }
        }

        public override void EmptyActionProcess ()
        {
            ActionProcess();
        }

        public override void GRRActionProcess ()
        {
            ActionProcess();
        }

        /// <summary>
        /// 工位步骤定义
        /// </summary>
        public enum StationStep
        {
            /// <summary>
            /// 等待机台初始化
            /// </summary>
            [Description("等待机台初始化")]
            开始准备,
            [Description("开始")]
            流程开始,
            轴初始位,
            等待上工位完成,
            等待放料,
            打开真空,
            克隆数据,
            等待上工位关完成,
            等待下工位开要料,
            轴翻转位,
            关闭真空,
            翻转到位,
            等待下工位关要料,
            轴到原位,
            通知翻转已离开,
            等待翻转工位放料结束,

            判断模式,

            GRR等待下工位完成,
            GRR翻转到位,
            GRR通知翻转已到位,
            GRR等待允许离开信号,
            GRR打开真空吸,
            GRR翻转到原位,
            GRR再次判断真空,
            GRR通知翻转已离开,
            GRR允许上料平台2取料,
            GRR下工位关闭要料,
            GRR关闭真空吸,
            GRR通知上料平台2可离开,
            GRR等待上料平台2已离开信号,

            [Description("工站完成")]
            流程结束,
        }

    }
}
